<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Models\Decode;
use App\Http\Models\Type;
use App\Http\Models\Video;
use Dotenv\Validator;
use Illuminate\Http\Request;

class VideoController extends Controller
{
    /**
     * 影视导航
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function video_type(Request $request)
    {
        $parent = $request->get('parent');
        $data = Type::lists($parent);
        if (!empty($data)){
            $json = ajax(200,'返回成功',$data);
        }else{
            $json = ajax(204,'暂无数据');
        }
        return $json;
    }


    /**
     * 影视列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function video_list(Request $request)
    {
        $param = $request->all();
        $data = Video::lists($param);
        if (!empty($data)){
            $json = ajax(200,'返回成功',$data);
        }else{
            $json = ajax(204,'暂无数据');
        }
        return $json;
    }

    /**
     * 获取影视详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse1
     */
    public function video(Request $request)
    {
        $id = $request->id;
        if (!$id) return ajax(412,'参数错误!');
        $data= Video::find($id);
        if ($data->video_type){
            $data['type_name'] = $data->video_type->name;
        }
        if ($data){
            $json = ajax(200,'返回成功',$data);
        }else{
            $json = ajax(203,'暂无数据');
        }
        return $json;
    }

    /**
     * 影视解析接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function video_decode(Request $request)
    {

        $validata =\Illuminate\Support\Facades\Validator::make($request->all(), [
            'url' => 'required|url',
        ]);
        if ($validata->fails()) {
            return ajax(402,'参数错误',$validata->errors()->first());
        }

        $url = $request->url;;
        if (!$url) return ajax(412,'参数错误!');
        $data = Decode::info();
        $apiUrl = $data['url'].'?'.$data['param'].'='.$url;
        if ($data){
            $json = ajax(200,'返回成功',$apiUrl);
        }else{
            $json = ajax(203,'暂无数据');
        }
        return $json;

    }
}
